﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;
namespace DemoForBillOcr
{
    class AutomationBillOcr
    {
        private BillOcrEngine billOcr = new BillOcrEngine();
        private const string DLLName = "billOcr.dll";
        private bool bIsInit = false;
        [DllImport(DLLName, EntryPoint = "InitRecogForm", CharSet = CharSet.Unicode)]
        public static extern int pInitRecogForm(string strUserID, string DllPath = null);
        [DllImport(DLLName, EntryPoint = "LoadTemplates", CharSet = CharSet.Unicode)]
        public static extern int pLoadTemplates(string strTemplate);
        [DllImport(DLLName, EntryPoint = "SetFormTypes", CharSet = CharSet.Unicode)]
        public static extern int pSetFormTypes(int[] TypeArray, int ArraySize);
        [DllImport(DLLName, EntryPoint = "LoadImages", CharSet = CharSet.Unicode)]
        public static extern int pLoadImages(string strImgName);
        [DllImport(DLLName, EntryPoint = "RecognizeForm", CharSet = CharSet.Unicode)]
        public static extern int pRecognizeForm(out int nCellCount, out int nTemplateType);
        [DllImport(DLLName, EntryPoint = "RecogUserDefinedForm", CharSet = CharSet.Unicode)]
        public static extern int pRecogUserDefinedForm(string strImgName, string strModName, out int nCellCount);
        [DllImport(DLLName, EntryPoint = "UninitRecogForm", CharSet = CharSet.Unicode)]
        public static extern int pUninitRecogForm();

        [DllImport(DLLName, EntryPoint = "GetRecognizeResult", CharSet = CharSet.Unicode)]
        public static extern int pGetRecognizeResult(int nFieldIndex, char[] strBuffer, out int nBufferLen);
        [DllImport(DLLName, EntryPoint = "GetCellType", CharSet = CharSet.Unicode)]
        public static extern int pGetCellType(int nFieldIndex, out int nFontType);
        ~AutomationBillOcr()
        {
            if (bIsInit)
            {
                UninitRecogForm();
            }
        }

        public int InitForm(string strUserID)
        {
            int initCode = -1;
            string struserIDNew = "0CCC56A614B779157F6E";
            initCode = pInitRecogForm(strUserID, null);
            if (0 != initCode)
            {
                initCode = pInitRecogForm(struserIDNew, null);
                if (0 != initCode)
                {
                    return initCode;
                }
                bIsInit = true;
                return initCode;
            }
            bIsInit = true;
            return initCode;
        }
        /*
         * 加载要识别的图像
         * strImgName--待识别图像名(绝对路径)
         * */
        public int LoadImage(string strImgName)
        {
            return billOcr.LoadImage(strImgName);
        }
        /*
         * 获取模板等级
         * lpModPath-用户自定义的模板名称(绝对路径) 
         * IsMulti_Mod-0 代表单模板、1 代表多模板 
         * */
        public int GetTemplateGrade(string lpModPath, out int IsMulti_Mod)
        {
            return billOcr.GetTemplateGrade(lpModPath, out IsMulti_Mod);
        }
        /*
         * 获取模板等级
         * lpFiledName-识别之前获取模板字段名称
         * */
        public int GetFieldNameEx(char[] lpFiledName)
        {
            return billOcr.GetFieldNameEx(lpFiledName);
        }
        /*
         * 识别条形码
         * nBarcodeType-条形码类型 
         * nCellCount-返回已识别图像对应的单元数目
         * */
        public int RecognizeBarcode(int nBarcodeType, out int nCellCount)
        {
            return billOcr.RecognizeBarcode(nBarcodeType, out nCellCount);
        }
        /**
         * 识别图像, UNICODE 函数 
         * nCellCount-返回已识别图像的识别单元数目 
         * nTemplateType-返回已识别图像对应的模板类别
         * */
        public int RecognizeForm(out int nCellCount, out int nTemplateType)
        {
            return billOcr.RecognizeForm(out nCellCount, out nTemplateType);
        }
        /**
         * 生成txt格式
         * strFileName-要保存的 xml 文件绝对路径名称 
         **/
        public int GetResultByTxt(string strFileName)
        {
            return billOcr.GetResultByTxt(strFileName);
        }
        /**
         * 生成xml格式
         * strFileName-要保存的 xml 文件绝对路径名称 
         **/
        public int GetResultByXml(string strFileName)
        {
            return billOcr.GetResultByXml(strFileName);
        }
        public int GetCellType(int nFieldIndex, out int nFontType)
        {
            return pGetCellType(nFieldIndex, out nFontType);
        }
        public int GetFieldName(int nFieldIndex, char[] strBuffer, out int nBufferLen)
        {
            return billOcr.GetFieldName(nFieldIndex, strBuffer, out nBufferLen);
        }
        public int GetRecognizeResult(int nFieldIndex, char[] strBuffer, out int nBufferLen)
        {
            return pGetRecognizeResult(nFieldIndex, strBuffer, out nBufferLen);
        }
        public int VerInvoice(string lpKey, string lpSecret, string lpInvoiceCode, string lpInvoiceNumber, string lpBillingDate, string lpTotalAmount, string lpCheckCode)
        {
            return billOcr.VerInvoice(lpKey, lpSecret, lpInvoiceCode, lpInvoiceNumber, lpBillingDate, lpTotalAmount, lpCheckCode);
        }
        public int GetErroInformation(int erroID, char[] lpBuffer, out int nBufferLen)
        {
            return billOcr.GetErroInformation(erroID, lpBuffer, out nBufferLen);
        }
        public int LoadTemplate(string strTemplateName)
        {
            return pLoadTemplates(strTemplateName);
        }
        public int SetFormTypes(int[] TypeArray, int nTypeSize)
        {
            return pSetFormTypes(TypeArray, nTypeSize);
        }
        public int RecogUserDefinedForm(string strImgName, string strModName, out int nCellCount)
        {
            return pRecogUserDefinedForm(strImgName, strModName, out nCellCount);
        }
        public int UninitRecogForm()
        {
            return pUninitRecogForm();
        }
        //识别图像返回类型
        public int AcquireAndRecogImage(string strImgName, out int nCellCount, out int nModID)
        {
            return billOcr.AcquireAndRecogImage(strImgName, out nCellCount, out nModID);
        }
        public int RecogBuildInForm(string strImgName, int nModID, out int nCellCount)
        {
            return billOcr.RecogBuildInForm(strImgName, nModID, out nCellCount);
        }
        //设置属性虑红
        public int SetDeviceAdvancedParams(int nParaType, int nParaValue)
        {
            return billOcr.SetDeviceAdvancedParams(nParaType, nParaValue);
        }
        public int AcquireNewImage(string strImgName)
        {
            return billOcr.AcquireNewImage(strImgName);
        }
        public bool IsHavePaper()
        {
            return billOcr.IsHavePaper();
        }
        //判断扫描前是否有纸
        public bool IfHavePaper()
        {
            return billOcr.IfHavePaper();
        }
    }
}
